//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.ServiceModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Core;
    using Preflight.Security;
    using Security;

    using Keys = PreflightServiceConfigurationSection.PropertyNames;

    internal class ServiceConfigurationReader : ConfigurationSectionReaderBase<PreflightServiceConfigurationSection>
    {
        protected override string ConfigurationSectionPath
        {
            get { return "preflight/service"; }
        }

        public virtual PreflightServiceOptions Read()
        {
            var section = this.GetSection();
            var options = CreateOptions(section);
            this.SetCustomAuthorizationFilterIfDefined(section, options);
            return options;
        }

        private void SetCustomAuthorizationFilterIfDefined(PreflightServiceConfigurationSection section, PreflightServiceOptions options)
        {
            if (options.SecurityMode == PreflightServiceSecurityMode.WindowsAuthentication)
            {
                return;
            }

            if (section.IsPropertyValueSupplied<string>(Keys.AuthorizationFilterTypeName))
            {
                options.CustomAuthorization = this.CreateInstanceFromConfiguredTypeName<IPreflightRequestAuthorization>(section, Keys.AuthorizationFilterTypeName);
            }
        }

        private static PreflightServiceOptions CreateOptions(PreflightServiceConfigurationSection section)
        {
            var options = new PreflightServiceOptions();
            section.InvokeActionIfPropertyAvailable<bool>(Keys.Enabled, value => options.Enabled = value);
            section.InvokeActionIfPropertyAvailable<string>(Keys.SecurityMode, value => options.SecurityMode = (PreflightServiceSecurityMode)Enum.Parse(typeof(PreflightServiceSecurityMode), value, true));
            section.InvokeActionIfPropertyAvailable<string>(Keys.CertificateLocation, value => options.CertificateLocation = CertificateLocation.Parse(value));
            section.InvokeActionIfPropertyAvailable<string>(Keys.Roles, value => options.Roles = ParseRoles(value));
            section.InvokeActionIfPropertyAvailable<string>(Keys.AcsIssuer, value => GetAcsOptions(options).Issuer = value);
            section.InvokeActionIfPropertyAvailable<string>(Keys.AcsAudience, value => GetAcsOptions(options).Audience = value);
            section.InvokeActionIfPropertyAvailable<string>(Keys.AcsSigningKey, value => GetAcsOptions(options).SigningKey = value);
            return options;
        }

        private static AcsOptions GetAcsOptions(PreflightServiceOptions options)
        {
            if (options.AcsOptions == null)
            {
                options.AcsOptions = new AcsOptions();
            }

            return options.AcsOptions;
        }

        private static IEnumerable<string> ParseRoles(string value)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return null;
            }

            var roles = value.Split(',');

            return roles.Select(item => item.Trim());
        }
    }
}
